package com.atgouwu.aiparent.springaialibaba.config;

import io.micrometer.observation.ObservationRegistry;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ApiKey;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.model.tool.ToolExecutionResult;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.List;

/**
 * Spring AI集成多个大语言模型，OpenAI，DeepSeek，Qwen，协议适配
 */
@Configuration
public class AiConfig {
    @Value("${spring.ai.openai.api-key}")
    private String openAiApiKey;
    @Value("${spring.ai.openai.base-url:https://api.openai.com}")
    private String openAiBaseurl;
    @Value("${spring.ai.openai.chat.options.model:gpt-3.5-turbo}")
    private String openAiModel;
    @Bean
    @Qualifier("openAiChatClientBuilder")
    public ChatClient.Builder openAiChatClientBuilder() {
        return ChatClient.builder(new OpenAiChatModel(
                new OpenAiApi(
                        openAiBaseurl,
                        new ApiKey() {
                            @Override
                            public String getValue() {
                                return openAiApiKey; // 修复API密钥使用
                            }
                        },
                        new LinkedMultiValueMap<>(),
                        "/v1/chat/completions",
                        "/v1/embeddings",
                        RestClient.builder(),
                        WebClient.builder(),
                        new DefaultResponseErrorHandler()
                ),
                OpenAiChatOptions.builder()
                        .model(openAiModel) // 使用从配置中获取的模型名称
                        .temperature(0.7)
                        .maxTokens(1000)
                        .build(),
                new ToolCallingManager() {
                    @Override
                    public List<ToolDefinition> resolveToolDefinitions(ToolCallingChatOptions chatOptions) {
                        return List.of();
                    }

                    @Override
                    public ToolExecutionResult executeToolCalls(Prompt prompt, ChatResponse chatResponse) {
                        return null;
                    }
                },
                RetryTemplate.builder().build(),
                ObservationRegistry.NOOP
        ));
    }

    @Value("${spring.ai.qwen.api-key}")
    private String qwenApiKey;
    @Value("${spring.ai.qwen.base-url:https://api.openai.com}")
    private String qwenBaseurl;
    @Value("${spring.ai.qwen.chat.options.model:gpt-3.5-turbo}")
    private String qwenModel;
    @Bean
    @Qualifier("qwenChatClientBuilder")
    public ChatClient.Builder qwenChatClientBuilder() {
        return ChatClient.builder(new OpenAiChatModel(
                new OpenAiApi(
                        qwenBaseurl,
                        new ApiKey() {
                            @Override
                            public String getValue() {
                                return qwenApiKey; // 修复API密钥使用
                            }
                        },
                        new LinkedMultiValueMap<>(),
                        "/v1/chat/completions",
                        "/v1/embeddings",
                        RestClient.builder(),
                        WebClient.builder(),
                        new DefaultResponseErrorHandler()
                ),
                OpenAiChatOptions.builder()
                        .model(qwenModel) // 使用从配置中获取的模型名称
                        .temperature(0.7)
                        .maxTokens(1000)
                        .build(),
                new ToolCallingManager() {
                    @Override
                    public List<ToolDefinition> resolveToolDefinitions(ToolCallingChatOptions chatOptions) {
                        return List.of();
                    }

                    @Override
                    public ToolExecutionResult executeToolCalls(Prompt prompt, ChatResponse chatResponse) {
                        return null;
                    }
                },
                RetryTemplate.builder().build(),
                ObservationRegistry.NOOP
        ));
    }

    @Value("${spring.ai.deepseek.api-key}")
    private String deepseekApiKey;
    @Value("${spring.ai.deepseek.base-url:https://api.openai.com}")
    private String deepseekBaseurl;
    @Value("${spring.ai.deepseek.chat.options.model:gpt-3.5-turbo}")
    private String deepseekModel;
    @Bean
    @Qualifier("deepseekChatClientBuilder")
    public ChatClient.Builder deepseekChatClientBuilder() {
        return ChatClient.builder(new OpenAiChatModel(
                new OpenAiApi(
                        deepseekBaseurl,
                        new ApiKey() {
                            @Override
                            public String getValue() {
                                return deepseekApiKey; // 修复API密钥使用
                            }
                        },
                        new LinkedMultiValueMap<>(),
                        "/v1/chat/completions",
                        "/v1/embeddings",
                        RestClient.builder(),
                        WebClient.builder(),
                        new DefaultResponseErrorHandler()
                ),
                OpenAiChatOptions.builder()
                        .model(deepseekModel) // 使用从配置中获取的模型名称
                        .temperature(0.7)
                        .maxTokens(1000)
                        .build(),
                new ToolCallingManager() {
                    @Override
                    public List<ToolDefinition> resolveToolDefinitions(ToolCallingChatOptions chatOptions) {
                        return List.of();
                    }

                    @Override
                    public ToolExecutionResult executeToolCalls(Prompt prompt, ChatResponse chatResponse) {
                        return null;
                    }
                },
                RetryTemplate.builder().build(),
                ObservationRegistry.NOOP
        ));
    }

}




